package cn.iocoder.yudao.module.erp.controller.admin.dp.dpalmt;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpalmt.vo.DpAlmtPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpalmt.vo.DpAlmtRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpalmt.vo.DpAlmtSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpAlmtDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemAlmtDO;
import cn.iocoder.yudao.module.erp.service.dp.DpAlmtService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - ERP阿拉木图仓库")
@RestController
@RequestMapping("/erp/dp-almt")
@Validated
public class DpAlmtController {

    @Resource
    private DpAlmtService dpAlmtService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP阿拉木图仓库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:create')")
    public CommonResult<String> createDpAlmt(@Valid @RequestBody DpAlmtSaveReqVO createReqVO) {
        return success(dpAlmtService.createDpAlmt(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP阿拉木图仓库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:update')")
    public CommonResult<Boolean> updateDpAlmt(@Valid @RequestBody DpAlmtSaveReqVO updateReqVO) {
        dpAlmtService.updateDpAlmt(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP阿拉木图仓库")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:delete')")
    public CommonResult<Boolean> deleteDpAlmt(@RequestParam("id") String id) {
        dpAlmtService.deleteDpAlmt(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP阿拉木图仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:query')")
    public CommonResult<DpAlmtRespVO> getDpAlmt(@RequestParam("id") String id) {
        DpAlmtDO dpAlmt = dpAlmtService.getDpAlmt(id);
        return success(BeanUtils.toBean(dpAlmt, DpAlmtRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP阿拉木图仓库分页")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:query')")
    public CommonResult<PageResult<DpAlmtRespVO>> getDpAlmtPage(@Valid DpAlmtPageReqVO pageReqVO) {
        PageResult<DpAlmtDO> pageResult = dpAlmtService.getDpAlmtPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpAlmtRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP阿拉木图仓库 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpAlmtExcel(@Valid DpAlmtPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpAlmtDO> list = dpAlmtService.getDpAlmtPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP阿拉木图仓库.xls", "数据", DpAlmtRespVO.class,
                        BeanUtils.toBean(list, DpAlmtRespVO.class));
    }

    @GetMapping("/dpAlmtOuterList")
    @Operation(summary = "获得ERP阿拉木图制单出库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:outer')")
    public CommonResult<PageResult<DpAlmtRespVO>> dpAlmtOuterList(@Valid DpAlmtPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DpAlmtDO> dpAlmtOuterList = dpAlmtService.dpAlmtOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpAlmtOuterList, DpAlmtRespVO.class));
    }

    @PostMapping("/dpAlmtOutStroe")
    @Operation(summary = "ERP阿拉木图仓库单据配车出库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:outer')")
    public CommonResult<Boolean> dpAlmtOutStroe(@Valid @RequestBody DispatchVehicleSaveReqVO createReqVO) {
        dpAlmtService.dpAlmtOutStroe(createReqVO);
        return success(true);
    }


    // ==================== 子表（ERP阿拉木图制单货号详情） ====================

    @GetMapping("/dp-item-almt/list-by-no")
    @Operation(summary = "获得ERP阿拉木图制单货号详情列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:detail')")
    public CommonResult<List<DpItemAlmtDO>> getDpItemAlmtListByNo(@RequestParam("no") String no) {
        return success(dpAlmtService.getDpItemAlmtListByNo(no));
    }

    @PostMapping("/enterStroeDpAlmt")
    @Operation(summary = "ERP运单入库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:enter')")
    public CommonResult<Boolean> enterStroeDpAlmt(@Valid @RequestBody List<DispatchVehicleSaveReqVO> createReqVO) {
        dpAlmtService.enterStroeDpAlmt(createReqVO);
        return success(true);
    }

    @GetMapping("/dpAlmtEnterList")
    @Operation(summary = "获得ERP制单入库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:enter')")
    public CommonResult<List<DispatchVehicleRespVO>> dpAlmtEnterList(@RequestParam(value = "dispatchNumber",required = false) String dispatchNumber,
                                                                   @RequestParam(value = "line",required = false) Integer line,
                                                                   @RequestParam(value = "dest",required = false) Integer dest,
                                                                     @RequestParam(value = "dpId",required = false) String dpId) {
        List<DispatchVehicleDO> dpAlmtEnterList = dpAlmtService.dpAlmtEnterList(dispatchNumber,line,1, dest,dpId);
        return success(BeanUtils.toBean(dpAlmtEnterList, DispatchVehicleRespVO.class));
    }

    @PostMapping("/signDp")
    @Operation(summary = "ERP到货签收")
//    @PreAuthorize("@ss.hasPermission('erp:dp-almt:enter')")
    public CommonResult<Boolean> signDp(@Valid @RequestBody DpAlmtSaveReqVO updateReqVO) {
        dpAlmtService.signDp(updateReqVO);
        return success(true);
    }

}